#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   mocks_result.py    
@Contact :   raogx.vip@hotmail.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2021/1/29 6:19 下午   gxrao      1.0         None
'''

# import lib
import random as rd
import logging

# 先配置一下日志级别
logging.basicConfig(level=logging.DEBUG)


def mock_sequence_list_by_confusion_matrix_info(sequence_list, matrix_info, need_add_rand=False):
    # 加载算法性能信息
    # True positive(TP): 真正例，将正类正确预测为正类数
    # False positive(FP): 假正例，将负类错误预测为正类数；
    # False negative(FN): 假负例，将正类错误预测为负类数；
    # True negative(TN): 真负例，将负类正确预测为负类数。

    mock_result = []
    tp = matrix_info["tp"]
    fp = matrix_info["fp"]
    fn = matrix_info["fn"]
    tn = matrix_info["tn"]
    positive_total = tp + fp
    negative_total = tn + fn

    tp_per = round(tp / positive_total, 4)
    tn_per = round(tn / negative_total, 4)
    if need_add_rand:
        tp_per -= round(rd.uniform(0, 0.1), 4)
        tn_per -= round(rd.uniform(0, 0.1), 4)
    '''
    logging.debug("tp=%s,fp=%s,fn=%s,tn=%s,tp_per=%s,tn_per=%s" % (tp, fp, fn, tn, tp_per, tn_per))
    '''

    for item in sequence_list:
        if item == 0:
            mock_negative_per = round(rd.uniform(0.4, 1), 4)
            '''
            if mock_negative_per>0.995:
                logging.debug("mock_negative_per=%s" % mock_negative_per)
            '''
            if mock_negative_per > tn_per:
                mock_result.append(1)
            else:
                mock_result.append(0)
        else:
            mock_positive_per = round(rd.uniform(0.4, 1), 4)
            if mock_positive_per > tp_per:
                mock_result.append(0)
            else:
                mock_result.append(1)

    return mock_result


def confusion_matrix_info_by_sequence_list(pred, real, classify):
    num = len(classify)
    base = int(round(len(pred) / num, 0))
    result = {}

    for i in range(0, num):
        tp = 0
        fp = 0
        tn = 0
        fn = 0
        for j in range(i * base, (i + 1) * base):
            if real[j] == 1:
                if real[j] == pred[j]:
                    tp += 1
                else:
                    fn += 1
            else:
                if real[j] == pred[j]:
                    tn += 1
                else:
                    fp += 1
        result[classify[i]] = {
            "tp": tp,
            "fp": fp,
            "tn": tn,
            "fn": fn,
        }

    return result
